#include "login.h"

void sendUserPasswd(int peerfd)
{
	while (1)
	{
		// 1.发送用户名
		puts("请输入用户名");
		char username[50] = {0};
		scanf("%s", username);
		sendArray(peerfd, username,strlen(username));

		//1.2验证用户名是否拥有
		char flag = -1;
		recv(peerfd,&flag,1,0);
		if (flag == 0)
		{
			printf("username error!\n");
			continue;
		}

		// 2.接收盐值
		int lenOfpass = 0;
		char salt[50] = {0};
		recvCircle(peerfd, (char *)&lenOfpass, 4);
		recvCircle(peerfd, salt, lenOfpass);

		printf("salt = %s\n",salt);
		// 3.输入密码，计算密文并传输
		puts("请输入密码");
		char passwd[100] = {0};
		scanf("%s", passwd);
		char *cryptograph = crypt(passwd, salt);
		printf("cryptograph = %s\n",cryptograph);
		sendMsg(peerfd, cryptograph);

		// 4.接收返回结果是否登陆成功
		recv(peerfd, &flag, 1, MSG_WAITALL);
		printf("flag = %d\n",flag);
		if (flag == 1)
		{
			printf("login success!\n");
			break;
		}
		else
		{
			printf("password error!\n");
		}
	}
}
